﻿using AcademiaInteligenteWeb.WsAcademiaInteligente;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AcademiaInteligenteWeb
{
    public partial class MainPlanoExercicioFrequencia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                txtData.Text = DateTime.Now.ToString("dd/MM/yyyy");
            grdPlanoExercicioFrequencia.DataKeyNames = new string[] { "id" };
            AtualizarGrid();
        }

        public void AtualizarGrid()
        {
            grdPlanoExercicioFrequencia.DataSource = new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().SelecionarPlanoExercicioFrequenciaPorPlanoExercicio(Convert.ToInt32(Request["id"]));
            grdPlanoExercicioFrequencia.DataBind();
        }
        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            DateTime inicio = Convert.ToDateTime(txtData.Text);
            int days = Convert.ToInt32(ddlQtdSemanas.SelectedValue) * 7;
            DateTime fim = inicio.AddDays(days);
            DateTime aux = inicio;
            List<int> daysofWeek = new List<int>();
            for (int i = 0; i < ckbListDiaSemana.Items.Count; i++)
            {
                if (ckbListDiaSemana.Items[i].Selected)
                    daysofWeek.Add(Convert.ToInt32(ckbListDiaSemana.Items[i].Value));
            }
            while (aux < fim)
            {
                if (daysofWeek.Contains((int)aux.DayOfWeek))
                {
                    Plano_Exercicio_Frequencia planoExercicioFrequencia = new Plano_Exercicio_Frequencia();
                    if (!string.IsNullOrEmpty(hfCurrentItem.Value))
                    {
                        planoExercicioFrequencia.id = Convert.ToInt32(hfCurrentItem.Value);
                    }
                    planoExercicioFrequencia.concluido = false;
                    planoExercicioFrequencia.id_plano_exercicio = Convert.ToInt32(Request["id"]);
                    planoExercicioFrequencia.data = aux;


                    if (planoExercicioFrequencia.id > 0)
                    {
                        //new WsAcademiaInteligenteSoapClient().(modalidade_Horario);
                    }
                    else
                    {
                        new WsAcademiaInteligenteSoapClient().InserirPlanoExercicioFrequencia(planoExercicioFrequencia);
                    }
                }
                aux = aux.AddDays(1);
            }
            AtualizarGrid();
        }
    }
}